#!/bin/bash

HARBOR_URL="http://10.202.16.6:8300"
USERNAME="lova-dev"
PASSWORD="Y3lova1234567"

projects=$(curl -u $USERNAME:$PASSWORD -s "$HARBOR_URL/api/projects")
for project in $(echo "$projects" | jq -r '.[].project_id'); do
    echo "Project ID: $project"

    # 获取项目名称
    project_name=$(echo "$projects" | jq -r --arg pid "$project" '.[] | select(.project_id == ($pid | tonumber)) | .name')

    # 获取每个项目下的所有镜像库
    repositories=$(curl -u $USERNAME:$PASSWORD -s "$HARBOR_URL/api/repositories?project_id=$project")

    # 遍历每个镜像库
    for repository in $(echo "$repositories" | jq -r '.[].name'); do
        echo "  Repository: $repository"

        # 获取每个镜像库下的所有镜像
        artifacts=$(curl -u $USERNAME:$PASSWORD -s "$HARBOR_URL/api/repositories/$project_name/$repository/artifacts")

        # 输出镜像的 ID 和大小
        echo "$artifacts" | jq -r '.[] | {id: .id, size: .size, pull_time: .pull_time}'
    done
done
